package org.tracy.timewindow;

import java.util.Date;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by dinghaiyun on 2016/11/29.
 */
public class TimeWindowQueue {
    //时间窗大小,可以设置，单位ms ,这里默认设置了1s
    public static Long TimeWindow = 1000l;

    private static BlockingQueue<TimeWindow> source = new LinkedBlockingQueue<>(1000);

    private static BlockingQueue<List> batchQueue = new LinkedBlockingQueue<>();

    public static void add(Object object) throws InterruptedException {
        TimeWindow timeWindow = new TimeWindow();
        timeWindow.setTime(new Date().getTime());
        timeWindow.setObject(object);
        source.put(timeWindow);
    }

    public static TimeWindow take() throws InterruptedException {
        return source.take();
    }

    public static void addBatchList(List batch) throws InterruptedException {
        batchQueue.put(batch);
    }

    public static List getBatchList() throws InterruptedException {
        return batchQueue.take();
    }
}
